/**
 * 流水账管理初始化
 */
var TBill = {
    id: "TBillTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

TBill.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left'},
        {
            title: '单号', field: 'bill_num', visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                if (d.searchflag == 0) {
                    return '<div style="background-color: #dddd00">' + d.bill_num + '</div>'
                }
                return d.bill_num;
            }
        },
        {title: '关联单号', field: 'income_name', visible: true, align: 'center', valign: 'middle'},
        {title: '结算来源', field: 'income_type', visible: true, align: 'center', valign: 'middle'},
        {title: '交易对象', field: 'traner', visible: true, align: 'center', valign: 'middle'},
        {title: '现结金额', field: 'money', visible: true, align: 'center', valign: 'middle'},
        {title: '现金', field: 'cash', visible: true, align: 'center', valign: 'middle'},
        {title: '微信', field: 'weixin', visible: true, align: 'center', valign: 'middle'},
        {title: '支付宝', field: 'alipay', visible: true, align: 'center', valign: 'middle'},
        {title: '银行卡', field: 'bank', visible: true, align: 'center', valign: 'middle'},
        {title: '挂账金额', field: 'zj_money', visible: true, align: 'center', valign: 'middle'},
        {title: '结算日期', field: 'cretime', visible: true, align: 'center', valign: 'middle'},
        {title: '记账日期', field: 'createtime', visible: true, align: 'center', valign: 'middle'},
        {title: '报账人', field: 'creater', visible: true, align: 'center', valign: 'middle'},
        {title: '状态', field: 'billCheckType', visible: true, align: 'center', valign: 'middle'}

    ];
};


/**
 * 检查是否选中
 */
TBill.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TBill.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加流水账
 */
TBill.openAddTBill = function () {
    var index = layer.open({
        type: 2,
        title: '添加流水账',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tBill/tBill_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看流水账详情
 */
TBill.openTBillDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '流水账详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tBill/tBill_update/' + TBill.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 确认对账
 */
TBill.confirmTBill = function () {
    if (ids.length > 0) {
        var ajax = new $ax(Feng.ctxPath + "/tBill/confirmTBill", function (data) {
            Feng.success("对账成功!");
            //TBill.table.refresh();
            document.location.reload();
        }, function (data) {
            Feng.error("对账失败!" + data.responseJSON.message + "!");
        });
        ajax.set("beginTime1", $("#beginTime").val());
        ajax.set("endTime1", $("#endTime").val());
        ajax.set("moneyAll", moneyAll);
        ajax.set("moneyNotPay", zj_money);//转结
        ajax.set("cash", $("#cash").val());
        ajax.set("weixin", $("#weixin").val());
        ajax.set("alipay", $("#alipay").val());
        ajax.set("bank", $("#bank").val());
        ajax.set("ids", ids.toString());
        ajax.start();
    } else {
        Feng.info("无对账数据!");
    }
};

/**
 * 查询流水账列表
 */
TBill.search = function () {
    var queryData = {};
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['code'] = $("#code").val();
    aTable.refresh(queryData);
};

//记录当前加载列表的id;
var ids = new Array();
var moneyAll = 0;
var zj_money = 0;
TBill.collectSum = function (m) {
    if (m) {
        mData = m;
    }
    var yjs = 0;//已结算
    var cash = 0;
    var alipay = 0;
    var weixin = 0;
    var bank = 0;

    for (var i = 0; i < mData.data.length; i++) {
        ids.push(mData.data[i].id);
        cash += parseFloat(mData.data[i].cash);
        alipay += parseFloat(mData.data[i].alipay);
        weixin += parseFloat(mData.data[i].weixin);
        bank += parseFloat(mData.data[i].bank);
        if (mData.data[i].zj_money != '--' && mData.data[i].zj_money != '') {
            zj_money += parseFloat(mData.data[i].zj_money);
        }
    }
    moneyAll = cash + alipay + weixin + bank;
    $('#cash').val(cash);
    $('#alipay').val(alipay);
    $('#weixin').val(weixin);
    $('#bank').val(bank);
    $('#zj_money').val(zj_money);
}

$(function () {
    var defaultColunms = TBill.initColumn();
    aTable = new LYTable(TBill.id, "/tBill/accountingList?condition=" + $("#condition").val() + "&beginTime=" + $("#beginTime").val(), defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TBill.table = aTable.initCallBack(mTable, TBill.collectSum);
    })
});
$(document).keyup(function (event) {
    if (event.keyCode == 13) {
        TBill.search();
    }
});
